package com.nstar.taobao
{
	import com.adobe.crypto.MD5;
	import com.nstar.base.util.DateUtils;
	import com.nstar.base.vo.ParamVO;
	
	import flash.utils.ByteArray;
	
	import mx.rpc.http.mxml.HTTPService;
	//import com.adobe.crypto.MD5;
	
	/**
	 * 淘宝AIP工具类
	 * 包括生成签名、拼接参数，以及调用接口方法
	 * @date : 2011.7.23
	 * @author : zhuchuanming
	 * 
	 */ 
	
	public class TaoBaoUtils
	{
		//正式环境的API调用地址
		private static const API_REST_URL:String = "http://gw.api.taobao.com/router/rest";
		
		//获取SessionKey的URL
		public static const SESSION_KEY_URL:String = "http://container.open.taobao.com/container?appkey=";
		
		public static const APP_KEY_PIM:String = "12353095";//12306094
		public static const APP_SECRET_PIM:String = "fc7195be5de75b560bbc93b217e0b170";//e23ad56aa8a899a5d52fa6873828015e
		
		
		public var service:HTTPService;
		public var appKey:String ;
		public var appSecret:String;
		
		public function TaoBaoUtils(service:HTTPService,appKey:String,appSecret:String)
		{
			this.service = service;
			this.appKey = appKey;
			this.appSecret = appSecret;
		}
		
		//生成签名
		private function createSign(params:Array,appSecret:String):String{
			//先根据key排序
			params = params.sortOn("key");
			//根据签名生成规则来生成签名, 密钥+(key/value)s+密钥
			var result:String = appSecret;
			for each(var param:ParamVO in params){
				if(param.key && param.value){
					result += param.key+param.value;
				}
			}
			result += appSecret;
			result = MD5.hash(result).toUpperCase();
			return result;
		}
		
		//生成参数串
		private function createStrParam(params:Array):String{
			var result:String = "";
			for each(var param:ParamVO in params){
				if(param.key && param.value){
					result += param.key+"="+param.value+"&";
				}
			}
			return result;
		}
		
		//调用淘宝方法
		public function call(params:Array,format:String="xml",img:ByteArray=null):void{
			if(params == null){
				params = new Array();
			}
			//一些固定参数
			/* 
			if(existsParam(params,"app_key")==false)
				params.push(ParamVO.createParam("app_key",appKey));
			if(existsParam(params,"format")==false)
				params.push(ParamVO.createParam("format",format));//xml/json
			if(existsParam(params,"sign_method")==false)
				params.push(ParamVO.createParam("sign_method","md5"));
			if(existsParam(params,"timestamp")==false)
				params.push(ParamVO.createParam("timestamp",DateUtils.format(new Date(),"YYYY-MM-DD HH:MS:SS")));
			if(existsParam(params,"v")==false)
				params.push(ParamVO.createParam("v","2.0"));
			 */
			pushParam(params,"app_key",appKey);
			pushParam(params,"format",format);
			pushParam(params,"sign_method","md5");
			pushParam(params,"timestamp",DateUtils.format(new Date(),"YYYY-MM-DD HH:MS:SS"));
			pushParam(params,"v","2.0");

			if(img){
				pushParam(params,"img",img);
			}
			
			//根据淘宝API调用规则拼接URL参数
			var sign:String = createSign(params,appSecret);//生成签名
			pushParam(params,"sign",sign);

			var request:Object = generateParamObject(params);
			service.url = API_REST_URL;
			service.send(request);
		}
		
		//简洁版调用
		public function invoke(fields:String,method:String,params:Array = null,
			session:String = null,format:String="xml",img:ByteArray=null):void{
			if(params==null){
				params = new Array();
			}
			pushParam(params,"fields",fields);
			pushParam(params,"method",method);
			pushParam(params,"session",session);
			//调用		
			call(params,format,img);
		}
		
		//设置参数，如果参数存在，则替换，否则新增参数
		public function pushParam(params:Array,paramName:String,paramValue:Object):void{
			var found:Boolean = false;
			for each(var param:ParamVO in params){
				if(param.key == paramName){
					found = true;
					param.value = paramValue;
					break;
				}
			}
			
			if(found == false){
				params.push(ParamVO.createParam(paramName,paramValue));
			}
		}
		
		private function generateParamObject(params:Array):Object{
			var result:Object = new Object();
			for each(var item:ParamVO in params){
				result[item.key] = item.value;
			}
			
			return result;
		}
		
		private function existsParam(params:Array,param:String):Boolean{
			for each(var item:Object in params){
				if(item.key == param){
					return true;
				}
			}
			return false;
		}
		
		
	}
}